﻿Imports System.IO
Imports System.Text
Imports System.IO.Compression
Imports ICSharpCode.SharpZipLib.GZip
Imports ICSharpCode.SharpZipLib.Tar
Imports System.Text.RegularExpressions

Public Class pathManager

    Shared Sub testFullAccessDirectory(path As String)
        Directory.GetDirectories(path)
        Directory.GetFiles(path)
        createDeleteRandomFile(path)
    End Sub

    Private Shared Sub createDeleteRandomFile(path As String)
        Dim randomgenerator1 As New Random
        Dim fileName = path & "\logMoverTestFile_" & StringGenerator.generate(10)

        'création / suppression
        File.Create(fileName).Close()
        File.Delete(fileName)
    End Sub

    Public Shared Sub compressFile(ByVal source As String, destination As String)
        ' Get the stream of the source file.
        Using inFile As FileStream = New FileStream(source, FileMode.Open)
            Using fs As New FileStream(destination, FileMode.Create, FileAccess.Write, FileShare.None)
                Using gzipStream As Stream = New GZipOutputStream(fs)
                    inFile.CopyTo(gzipStream)
                End Using
            End Using
        End Using
           
    End Sub

    Public Shared Function CopyDirectory(SourcePath As String, DestinationPath As String, overwriteexisting As Boolean) As Boolean
        Dim ret As Boolean = False

        Try
            SourcePath = If(SourcePath.EndsWith("\"), SourcePath, SourcePath & "\")
            DestinationPath = If(DestinationPath.EndsWith("\"), DestinationPath, DestinationPath & "\")

            If Directory.Exists(SourcePath) Then
                If Directory.Exists(DestinationPath) = False Then
                    Directory.CreateDirectory(DestinationPath)
                End If

                For Each fls As String In Directory.GetFiles(SourcePath)
                    Dim flinfo As New FileInfo(fls)
                    flinfo.CopyTo(DestinationPath + flinfo.Name, overwriteexisting)
                Next
                For Each drs As String In Directory.GetDirectories(SourcePath)
                    Dim drinfo As New DirectoryInfo(drs)
                    If CopyDirectory(drs, DestinationPath + drinfo.Name, overwriteexisting) = False Then
                        ret = False
                    End If
                Next
            End If
            ret = True
        Catch ex As Exception
            ret = False
        End Try
        Return ret
    End Function

    Private Sub compressDirectory(ByVal outputTarGZFilename As String, ByVal sourceDirectory As String)
        Using fs As New FileStream(outputTarGZFilename, FileMode.Create, FileAccess.Write, FileShare.None)
            Using gzipStream As Stream = New GZipOutputStream(fs)
                Using tarArchive__1 As TarArchive = TarArchive.CreateOutputTarArchive(gzipStream)
                    tarArchive__1.AddDirectoryFilesToTar(sourceDirectory, True)
                End Using
            End Using
        End Using
    End Sub

    Public Shared Function getFilesToMove(fichiers As FileInfo(), nbJourRetention As Integer) As List(Of FileInfo)
        Dim out = (From fic In fichiers
                   Select fic Where fic.getDateFromName <> Nothing AndAlso fic.getDateFromName.AddDays(nbJourRetention) < Now _
                   Order By fic.getDateFromName).ToList
        Return out
    End Function

    Public Shared Function getFoldersToMove(fichiers As DirectoryInfo(), nbJourRetention As Integer) As List(Of DirectoryInfo)
        Dim out = (From doss In fichiers
                   Select doss Where doss.getDateFromName <> Nothing AndAlso doss.getDateFromName.AddDays(nbJourRetention) < Now _
                   Order By doss.getDateFromName).ToList
        Return out
    End Function

End Class

Class StringGenerator

    Public Shared Function generate(size As Integer) As String
        Dim legalCharacters As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
        Dim random As New Random()

        Dim builder As New StringBuilder()
        Dim ch As Char

        For i As Integer = 0 To size - 1
            ch = legalCharacters(random.[Next](0, legalCharacters.Length))
            builder.Append(ch)
        Next

        Return builder.ToString()
    End Function

End Class

